package com.kairos.jetparksample.ui

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.kairos.basecomponent.BaseActivity
import com.kairos.jetparksample.R
import com.kairos.jetparksample.domain.User
import com.kairos.jetparksample.databinding.ActivityMainBinding
import com.kairos.jetparksample.domain.SaveStateViewModelSample
import com.kairos.jetparksample.domain.ViewModelSample

class MainActivity : AppCompatActivity() {

    val viewModelSample: ViewModelSample
         by lazy {
             /**
              * owner: 确定ViewModel的作用域，
              * factory：创建ViewModel实例，可自定义
              */
             ViewModelProvider(this, defaultViewModelProviderFactory)[ViewModelSample::class.java]
        }

    private val viewModel: ViewModelSample by viewModels()


    //有参数的ViewModel初始化
    private val applicationViewModel: SaveStateViewModelSample by viewModels {
        SaveStateViewModelSample.Factory
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        /**
         * 1、初始化binding对象
         */

        val activityMainBinding =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        activityMainBinding.user = User("nick", "10086", null)

        activityMainBinding.userinfoTxtLogout.setOnClickListener {
            startActivity(Intent(this@MainActivity, BaseActivity::class.java))
        }
//        activityMainBinding.user = null
//        activityMainBinding.user?.userName = null


//        val userCaseObserver = Observer<User> { user ->
//            //UI update
//            user.userName?.let {
//                userinfo_txt_nickname.setText(it)
//            }
//            user.mobile?.let {
//                userinfo_txt_mobile.text = it
//
//            }
//            user.head?.let {
//                Glide.with(this).load(R.mipmap.head).into(userinfo_img_head)
//            }
//
//            userinfo_txt_isbindwx.text = user.wx
//        }
//        viewModel.userInfo.observe(this, userCaseObserver)
        //永久观察，通过removeObserver移除
        //viewModel.userInfo.observeForever(userCaseObserver)
        //请求数据
        viewModel.requestSampleData()

    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)

    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)

    }
}